// packageI/pages/storageAdd/storageAdd.js/
const app = getApp()
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    warehouseId: '',
    imgUrl: applet_park_api_url_imgUrl,
    token: wx.getStorageSync('TOKEN'),
    infos: '',
    banners: [],
    num: '',
    price: '',
    text: '',
    billType: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.data.billType = options.type
    that.data.warehouseId = options.warehouseId
    that.setData(that.data)
    that.getInfos()

  },
  getInfos() {
    var that = this
    wx.request({
      url: applet_park_api_url + '/api/consumable/' + that.data.warehouseId,
      method: 'GET',
      success: function (res) {
        if (res.data.code == 0) {
          that.data.infos = res.data.data
          that.data.banners = res.data.data.images.split(",")
          that.setData(that.data)
        }
      }
    })
  },
  changeNum(e) {
    var that = this
    that.data.num = e.detail.value
    that.setData(that.data)
  },
  changePrice(e) {
    var that = this
    that.data.price = e.detail.value
    that.setData(that.data)
  },
  changeText(e) {
    var that = this
    that.data.text = e.detail.value
    that.setData(that.data)
  },
  submit() {
    var that = this
    if (that.data.num == 0 || that.data.num == '') {
      wx.showToast({
        title: '请输入采购数量',
      })
      return
    }
    if (that.data.text == '') {
      wx.showToast({
        title: '请输入备注',
      })
      return
    }
    if (that.data.billType == 0) { //采购
      if (that.data.price == 0 || that.data.price == '') {
        wx.showToast({
          title: '请输入采购单价',
        })
        return
      }
      wx.request({
        url: applet_park_api_url + '/api/consumableBillDetail',
        method: 'POST',
        header: {
          Authorization: that.data.token
        },
        data: {
          consumableId: that.data.infos.id,
          billType: that.data.billType,
          price: that.data.price,
          quantity: that.data.num,
          remark: that.data.text,
          images: that.data.infos.images
        },
        success: function (res) {
          if (res.data.code == 0) {
            wx.navigateBack()
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
        }
      })
    } else if (that.data.billType == 1) { //取用
      if (that.data.infos.totalQuantity - that.data.num < 0) {
        wx.showToast({
          title: '库存数量不足',
          icon: 'none'
        })
        return
      }
      wx.request({
        url: applet_park_api_url + '/api/consumableBillDetail/outWarehouseOrder',
        method: 'POST',
        header: {
          Authorization: that.data.token
        },
        data: {
          consumableId: that.data.infos.id,
          quantity: that.data.num,
          remark: that.data.text
        },
        success: function (res) {
          if (res.data.code == 0) {
            wx.navigateBack()
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '未知类型',
        icon: "none"
      })
      return
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})